/*
 * This file is part of Keepass2Android, Copyright 2025 Philipp Crocoll.
 *
 *   Keepass2Android is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Keepass2Android is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Keepass2Android.  If not, see <http://www.gnu.org/licenses/>.
 */

package keepass2android.kp2afilechooser;

import java.util.List;

import com.crocoapps.javafilestoragetest2.MainActivity;

public class StorageFileProvider extends Kp2aFileProvider {

	public static String authority = "keepass2android.kp2afilechooser.StorageFileProvider";

	@Override
	public String getAuthority() {
		return authority;
	}

	@Override
	protected FileEntry getFileEntry(String path, StringBuilder errorMessageBuilder) throws Exception {

			keepass2android.javafilestorage.JavaFileStorage.FileEntry entry = MainActivity.storageToTest.getFileEntry(path);
			keepass2android.kp2afilechooser.FileEntry chooserEntry = convertEntry(entry);
			return chooserEntry;

	}

	private keepass2android.kp2afilechooser.FileEntry convertEntry(
			keepass2android.javafilestorage.JavaFileStorage.FileEntry entry) {
		keepass2android.kp2afilechooser.FileEntry chooserEntry = new FileEntry();
		chooserEntry.canRead = entry.canRead;
		chooserEntry.canWrite = entry.canWrite;
		chooserEntry.displayName = entry.displayName;
		chooserEntry.isDirectory = entry.isDirectory;
		chooserEntry.lastModifiedTime = entry.lastModifiedTime;
		chooserEntry.path = entry.path;
		chooserEntry.sizeInBytes = entry.sizeInBytes;
		return chooserEntry;
	}

	@Override
	protected void listFiles(int taskId, String dirName,
			boolean showHiddenFiles, int filterMode, int limit,
			String positiveRegex, String negativeRegex,
			List<keepass2android.kp2afilechooser.FileEntry> results, boolean[] hasMoreFiles) {
		
		List<keepass2android.javafilestorage.JavaFileStorage.FileEntry> entries;
		try {
			entries = MainActivity.storageToTest.listFiles(dirName);
			for (keepass2android.javafilestorage.JavaFileStorage.FileEntry e: entries)
			{
				keepass2android.kp2afilechooser.FileEntry chooserEntry = convertEntry(e);
				results.add(chooserEntry);
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		

	}

	@Override
	protected boolean deletePath(String filename, boolean isRecursive) {
		try
		{
			MainActivity.storageToTest.delete(filename);
			return true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	@Override
	protected boolean createDirectory(String dirname, String newDirName) {
		try
		{
			MainActivity.storageToTest.createFolder(dirname, newDirName);
			return true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

}
